import http from '@/utils/request'
import { removeTokenInfo, setTokenInfo } from '@/utils/storage'
/**
 * 发送短信验证码
 * @param {Number} mobile  手机号码
 * @returns
 */
export const sendValidationCode = (mobile) => {
  return async (dispatch) => {
    const res = await http(`/sms/codes/${mobile}`, 'get')
    console.log(res)
  }
}

// 存token
export const saveToken = (token) => {
  return {
    type: 'login/token',
    payload: token,
  }
}

/**
 * 登录
 * @param {Number} mobile  手机号码
 * @param {Number} code  验证码
 * @returns
 */

export const login = (mobile, code) => {
  return async (dispatch) => {
    const { data } = await http('/authorizations', 'post', { mobile, code })
    // 将token数据保存到localstorage中
    setTokenInfo(data)
    // 数据存储到redux中
    dispatch(saveToken(data))
  }
}

// 退出登录
export const logout = () => {
  // 清除缓存
  removeTokenInfo()
  // 清除redux中的数据
  return saveToken({})
}
